<?php
defined('IN_IA') or exit('Access Denied');

global $_W, $_GPC;
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
//知道列表页
if ($op == 'display') {
    $condition = " uniacid={$_W['uniacid']} ";
    $params = array();
    $type = (int)($_GPC['type']);
    $keyword = trim($_GPC['keyword']);
    if (!empty($type)&&$type!=0){
        $condition .= " AND categoryid ={$type}";
    }
    if (!empty($keyword)){
        $condition .= " AND title LIKE :title";
        $params[':title'] = "%{$keyword}%";
    }
    $pindex = max(1, intval($_GPC['page']));
    //当前页码
    $psize = 10;
    //设置分页大小
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('aide_know')." WHERE $condition", $params);
    $sqlData = "SELECT * FROM " . tablename ('aide_know') . " WHERE $condition ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize;
    $know = pdo_fetchall ($sqlData,$params);
    $temp=pdo_fetchall("SELECT id,name FROM " . tablename('aide_category') . " WHERE parentid=5 ORDER BY  id ASC");
    foreach ($temp as $value){
        $category[$value['id']]=$value;
    }
    //记录总数
    $pager = pagination($total, $pindex, $psize);
    include aide_template ('know');
    exit;
}

if ($op == 'post') {
    $knowid = intval($_GPC['knowid']);
    $know=pdo_fetchall("SELECT * FROM " . tablename('aide_know') . " WHERE id=$knowid ORDER BY  id ASC LIMIT 1");
    $category=pdo_fetchall("SELECT * FROM " . tablename('aide_category') . " WHERE parentid=5 ORDER BY id ASC");
    $know=$know[0];
    $time=date('Y-m-d H:i:s');
    if (checksubmit('submit')) {
        $form = $_GPC['know'];
        $data['uniacid'] = $_W['uniacid'];
        $data['title'] = $form['title'];
        $data['image'] = $form['image'];
        $data['intro'] = $form['intro'];
        $data['categoryid'] = $form['categoryid'];
        $data['updated_at'] = $time;
        //协议字数处理
        $data['content'] = html_format($form['content']);
        $bytes = strlen(html_format($data['content']))-65000;
        if ($bytes > 0){
            message('协议内容已超出存储范围：约超' . round($bytes/3) . "个汉字");
        }
        if (!empty($knowid)) {
            pdo_update ('aide_know', $data, array ('id' => $knowid));
        } else {
            $data['created_at'] = $time;
            pdo_insert ('aide_know', $data);
            $activityid = pdo_insertid();
        }
        message ('更新成功！', web_url('know/know/display'), 'success');
    }
    include aide_template ('know');
    exit;
}

if ($op == 'delete') {
    $knowid = intval($_GPC['knowid']);
    pdo_delete ('aide_know', array ('id' => $knowid));
    die(json_encode(array("errno" => 0,'message'=>'删除成功')));
    exit;
}
